<?php
class CMemberattr extends Zend_Db_Table
{
	protected $_name;
	protected $_primary;

	public function __construct()
	{
		$this->_name = R_PRE."member_attr";
		$this->_primary = "member_attr_id";
		parent::__construct();
		
	}
	
	public function getAll($page, $pageNum, $type, $uid = 0)
	{
		$where = array();
		if ($type != 5) {
			$where['statu = ?'] = $type;
		}
		if ($uid) {
			$where['uid = ?'] = $uid;
		}
		$offset = ($page - 1) * $pageNum;
		
		return $this->fetchAll($where, 'add_time desc', $pageNum, $offset);
	}
	
	public function getPageCount($pageNum, $type, $uid = 0)
	{
		$where = array();
		if ($type != 5) {
			$where['statu = ?'] = $type;
		}
		if ($uid) {
			$where['uid = ?'] = $uid;
		}
		$rows = $this->fetchAll($where);		
		return count($rows);
	}
	
	public function getAttr($attrName, $uid)
	{
		return $this->fetchRow(array('attr_name = ?'=>$attrName, 'uid = ?'=>$uid));
	}
	
	public function updateSns($sns, $snsValue, $uid)
	{
		if ($row = $this->getAttr($sns, $uid)) {
			return $this->update(array('attr_value'=>$snsValue), 'member_attr_id = ' . intval($row->member_attr_id));
		}
		return $this->insert(array('attr_name'=>$sns, 'attr_value'=>$snsValue, 'uid'=>$uid, 'add_time'=>time()));
	}
}